package cn.wolfcode.luowowo.hotel.service.impl;

import cn.wolfcode.luowowo.hotel.domain.Hotel;
import cn.wolfcode.luowowo.hotel.domain.HotelRoomType;
import cn.wolfcode.luowowo.hotel.mapper.HotelMapper;
import cn.wolfcode.luowowo.hotel.mapper.HotelOrderMapper;
import cn.wolfcode.luowowo.hotel.mapper.HotelRoomTypeMapper;
import cn.wolfcode.luowowo.hotel.query.HotelQueryObject;
import cn.wolfcode.luowowo.hotel.service.IHotelService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 酒店服务实现
 *
 * @author win
 */
@Service
public class HotelServiceImpl implements IHotelService {

    @Autowired
    private HotelMapper hotelMapper;

    @Autowired
    private HotelRoomTypeMapper hotelRoomTypeMapper;

    @Autowired
    private HotelOrderMapper hotelOrderMapper;

    @Override
    public void deleteById(Long id) {
        hotelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Hotel record) {
        if (record.getId() != null) {
            hotelMapper.updateByPrimaryKey(record);
        } else {
            hotelMapper.insert(record);
        }
    }

    @Override
    public Hotel findById(Long id) {
        return hotelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Hotel> selectAll() {
        return hotelMapper.selectAll();
    }

    @Override
    public PageInfo<Hotel> query(HotelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo<>(hotelMapper.selectForList(qo));
    }

    @Override
    public List<Hotel> queryHotelCitySpecial(boolean whetherSpecialPrice) {
        if (whetherSpecialPrice) {
            return hotelMapper.selectByWhetherSpecialPrice(Hotel.HOTEL_SPECIAL);
        }
        return hotelMapper.selectByWhetherSpecialPrice(Hotel.HOTEL_ON_SPECIAL);
    }

    @Override
    public List<Hotel> queryByThemeId(Long themeId) {
        List<Hotel> hotels = hotelMapper.selectByThemeId(themeId);
        return hotels == null ? Collections.emptyList() : hotels;
    }

    @Override
    public List<Hotel> queryByDestId(Long destId) {
        List<Hotel> hotels = hotelMapper.selectByDestId(destId);
        return hotels == null ? Collections.emptyList() : hotels;
    }

    @Override
    public List<Hotel> queryByDestinationId(HotelQueryObject qo) {
        return hotelMapper.selectByDestinationId(qo);
    }

    @Override
    public Hotel queryByHotelId(HotelQueryObject qo) {
        return hotelMapper.selectByPrimaryKey(qo.getHotelId());
    }

    @Override
    public Map<String, Object> thereRoom(HotelQueryObject qo) {
        // 查询当前酒店各类住房的数量
        List<HotelRoomType> hotelRoomTypes = hotelRoomTypeMapper.selectByHotelId(qo);
        // 根据指定的时间查询当前酒店订出的房间
        List<Map<String, Object>> list = hotelOrderMapper.selectOrderGroupBy(qo);
        Map<String, Object> temp = new HashMap<>();
        for (HotelRoomType roomType : hotelRoomTypes) {
            temp.put(roomType.getType().toString(), roomType.getRooms());
        }
        Set<String> set = temp.keySet();
        for (Map<String, Object> map : list) {
            for (String aLong : set) {
                if (map.get("roomType").toString().equals(aLong)) {
                    temp.put(aLong, (Integer.valueOf(temp.get(aLong) + "") - Integer.valueOf(map.get("orderRoom") + "")));
                    break;
                }
            }
        }
        // 判断是否有房间
        return temp;
    }

    @Override
    public List<Hotel> list() {
        return hotelMapper.selectAll();
    }
}
